package com.generic.mybatis.dal.interceptor;

import com.generic.mybatis.dal.dao.GenericOperation;
import com.generic.mybatis.dal.interceptor.automap.SqlBuilder;
import com.generic.mybatis.dal.interceptor.automap.SqlBuilderFactory;
import com.generic.mybatis.dal.interceptor.automap.builder.*;
import com.google.common.collect.Maps;

import java.util.Map;

/**
 * 提供构造Sql方的Builder类
 * 根据方法各提供构造
 *
 * @author wuaj
 */
public class SqlBuilderFactoryDefault implements SqlBuilderFactory {

    private final Map<String, SqlBuilder> builderMap = Maps.newHashMap();

    public SqlBuilderFactoryDefault() {
        builderMap.put(GenericOperation.COUNT, new CountBuilder());
        builderMap.put(GenericOperation.DELETE, new DeleteBuilder());
        builderMap.put(GenericOperation.DELETE_BY_ID, new DeleteByIdBuilder());
        builderMap.put(GenericOperation.FIND, new FindBuilder());
        builderMap.put(GenericOperation.FIND_ONE, new FindBuilder());
        builderMap.put(GenericOperation.FIND_LOCK, new FindLockBuilder());
        builderMap.put(GenericOperation.FIND_ONE_LOCK, new FindLockBuilder());
        builderMap.put(GenericOperation.FIND_ALL, new FindAllBuilder());
        builderMap.put(GenericOperation.FIND_BY_PAGE, new FindByPageBuilder());
        builderMap.put(GenericOperation.GET_ONE, new GetOneBuilder());
        builderMap.put(GenericOperation.GET_ONE_BY_ID, new GetOneByIdBuilder());
        builderMap.put(GenericOperation.INSERT, new InsertBuilder());
        builderMap.put(GenericOperation.UPDATE, new UpdateBuilder());
        builderMap.put(GenericOperation.SUM, new SumBuilder());
    }

    @Override
    public SqlBuilder getBuilder(String name) {
        SqlBuilder builder = builderMap.get(name);
        if (builder == null) {
            throw new IllegalArgumentException(String.format("sql builder of %s is not found", name));
        }
        return builder;
    }
}
